#!/usr/bin/env node

const { validateProbabilityInput, calculateEntropy, interactiveInput } = require('./entropyCalculation')

const args = process.argv.slice(2)

async function main() {
  let probabilities

  if (args.length === 0) {
    console.log('请输入信源概率分布: (Ctrl + D 退出输入)')
    probabilities = await interactiveInput()
  } else {
    const { isValid, errors, probabilities: parsedProbabilities} = validateProbabilityInput(args)
    
    if (!isValid) {
      console.log(errors[0])
      console.log('请输入信源概率分布: (Ctrl + D 退出输入)')
      probabilities = await interactiveInput()
    } else {
      probabilities = parsedProbabilities
    }
  }
  
  const entropy = calculateEntropy(probabilities)
  console.log(`\np = [${probabilities}]\n`)
  console.log(`h(p) = ${entropy.toFixed(3)} bit`)
}

main()
